﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HtmlAgilityPack;
using System.Net;
using System.Collections;
using System.Threading;

namespace GetProxyList
{
    class Program
    {
        static string lockVar = string.Empty;

        static void Main(string[] args)
        {
            ThreadPool.SetMaxThreads(10, 10);
            getProxiesList();

            Console.ReadLine();
        }

        static void cPool(object state)
        {
            bool isCanUse = checkProxy(state.ToString());
            Console.WriteLine(state.ToString() + "\t" + isCanUse.ToString());
        }

        private static void getProxiesList()
        {
            for (int i = 1; i < 11; i++)
            {
                string url = string.Format(Properties.Resources.BaseProxyUrl, i);
                var wClient = new WebClient();
                HtmlDocument doc = new HtmlDocument();
                doc.LoadHtml(wClient.DownloadString(url));
                Dictionary<string, char> cTable = new Dictionary<string, char>();
                var cArray = doc.DocumentNode.SelectSingleNode("html/head/script").InnerHtml.Remove(0, 1).Split(new char[] { ';' });
                foreach (var c in cArray)
                {
                    if (string.IsNullOrEmpty(c))
                        continue;

                    var a = c.Split(new char[] { '=' });
                    cTable.Add(a[0], a[1][1]);
                }

                var content = doc.GetElementbyId("proxylisttb");
                var table = content.SelectSingleNode("table[3]");
                var proxies = table.SelectNodes(".//tr");
                proxies.Remove(0);
                foreach (var item in proxies)
                {
                    StringBuilder ipStrBuilder = new StringBuilder();
                    var ip = item.FirstChild.Element("#text").InnerHtml;
                    ipStrBuilder.Append(ip);
                    ipStrBuilder.Append(':');
                    var wArray = item.FirstChild.Element("script").InnerHtml.Replace(")", string.Empty).Split(new char[] { '+' });
                    for (int j = 1; j < wArray.Length; j++)
                    {
                        ipStrBuilder.Append(cTable[wArray[j]]);
                    }

                    ThreadPool.QueueUserWorkItem(new WaitCallback(cPool), ipStrBuilder.ToString());
                }
            }
        }

        static bool checkProxy(string proxy)
        {
            var wClient = new MyWebClient() { Timeout = 5 };
            try
            {
                wClient.Proxy = new WebProxy("http://" + proxy);
                string result = wClient.DownloadString("http://www.google.com");
                if (string.IsNullOrEmpty(result))
                {
                    return false;
                }
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
    }
}
